package com.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.pojo.Image;
import com.util.JdbcUtils;

public class UploadDao {
	
	private QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
	
	
	public int saveImage(Map<String,String> map) throws SQLException{
		String sql = "insert into image(username, pictureFile) values (?, ?)";
		Object[] param = {map.get("username"),map.get("pictureFile")};
		int i = qr.update(sql, param);
		return i;
	}
	
	public List<Image> getImage() throws SQLException {
		String sql = "select * from image";
		List<Image> list = qr.query(sql,new BeanListHandler<>(Image.class));
		return list;
	}

	public String selectImageByName(String name) throws SQLException {
		String sql = "select * from image where username = ?";
		Object[] param = {name};
		String pictureFile = null;
		List<Image> list = qr.query(sql, new BeanListHandler<>(Image.class),param);
		for (Image image : list){
			pictureFile = image.getPictureFile();
		}
		return pictureFile;
	}
	
	/*public int saveImage(Map<String,String> image){
		Connection conn = null;
		PreparedStatement ps = null;
		int result = 0;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/exam", "root", "wwh1122331996a");
			String sql = "insert into image(username, pictureFile) values (?, ?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, image.get("username"));
			ps.setString(2, image.get("pictureFile"));
			result = ps.executeUpdate();
			
			
		} catch (ClassNotFoundException e) {
			System.out.println("加载驱动问题");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("连接数据库问题");
			e.printStackTrace();
		}finally
		{
			try
			{

				ps.close();

				conn.close();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}

		return result;
	}

	public List<Image> getImage() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet result = null;
		List<Image> list = new ArrayList<>();
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/exam", "root", "wwh1122331996a");
			String sql = "select * from image";
			ps = conn.prepareStatement(sql);
			result = ps.executeQuery();
			
			
			Image image = null;
			
			while(result.next())
			{
				
				String username = result.getString("username");
				
				String imagefile = result.getString("pictureFile");
				
				image = new Image(username, imagefile);
				
				list.add(image);
			}
			
		} catch (ClassNotFoundException e) {
			System.out.println("加载驱动问题");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("连接数据库问题");
			e.printStackTrace();
		}finally
		{
			try
			{

				ps.close();

				conn.close();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}

		return list;
	}*/
}
